package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.server.UserRoleServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.UnitReportDetailActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.AddResourceObj
import cc.vv.btpartyjob.partymanager.moudel.bean.AddSubmitResourceObj
import cc.vv.btpartyjob.partymanager.moudel.req.UnitReportAddResourceRequestObj
import cc.vv.btpartyjob.partymanager.moudel.resp.SubmitUnitReportResponseObj
import cc.vv.btpartyjob.partymanager.moudel.resp.UnitReportDetailResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 单位报到详情
 * Created by Chen.qi on 2018/10/27.
 */
class UnitReportDetailActivity : BaseActivityMVP<UnitReportDetailActivityDelegate>() {
    /**
     * 资源集合
     */
    private var resourceList: ArrayList<AddResourceObj>? = null
    /**
     * 新增资源集合
     */
    private var newL: ArrayList<AddSubmitResourceObj>? = null
    /**
     * 单位id
     */
    private var unitId: String? = null

    override fun initData() {
        viewDelegate?.setTopBarTitle("单位报到详情")
        resourceList = ArrayList()
        newL = ArrayList()
        unitId = intent.getStringExtra("unitId")
        if (UserRoleServer.getUnitRole()) {
            viewDelegate.showAdd(true)
            viewDelegate.showSubmit(true)
        }
        PartyManagerServer.unitReportDetail(unitId, true)


    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {

            }

            override fun rightOnClick() {

            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //新增资源
            val intent = Intent(this, AddResourceActivity::class.java)
            startActivityForResult(intent, 100)
        }, R.id.rl_auRd_addResource)
    }

    override fun getDelegateClass(): Class<UnitReportDetailActivityDelegate> {
        return UnitReportDetailActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is UnitReportDetailResponseObj -> {
                val data = obj.data
                viewDelegate?.setUnitName(data?.unitName)
                viewDelegate?.setPrincipalName(data?.principal)
                viewDelegate?.setPartyPerNum(data?.partyNum)
                viewDelegate?.setUnitAddress(data?.unitAddr)
                viewDelegate?.setUnitCode(data?.zipCode)
                viewDelegate?.setStreetName(data?.street)
                viewDelegate?.setCommunityName(data?.community)
                viewDelegate?.setReportTime(data?.reportTime)
                if (null != data?.resourceList) {
                    resourceList!!.addAll(data.resourceList!!)
                }
                viewDelegate?.setListData(resourceList)

                if (UserRoleServer.getUnitRole()) {
                    viewDelegate?.showSubmit(false)
                    viewDelegate?.showAdd(true)
                }

            }

            is SubmitUnitReportResponseObj -> {
                LKToastUtil.showToastShort("资源新增成功")
//                PartyManagerServer.unitReportDetail(unitId, false)
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (obj?.statusCode == 100001) {
            if (UserRoleServer.getUnitRole()) {
                val intent = Intent(this, AddUnitReportActivity::class.java)
                startActivity(intent)
                finish()
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val resourceObj = data.getSerializableExtra("ADD_RESOURCE") as AddResourceObj
            val subRe = AddSubmitResourceObj()
            val longTime = LKTimeUtil.getInstance().getLongTime(resourceObj.startTime, "yyyy-MM-dd")
            val endTime = LKTimeUtil.getInstance().getLongTime(resourceObj.endTime, "yyyy-MM-dd")
            subRe.startTime = longTime
            subRe.endTime = endTime
            subRe.amount = resourceObj.amount
            subRe.conetnt = resourceObj.conetnt
            subRe.num = resourceObj.num
            subRe.reward = resourceObj.reward
            subRe.unitId = unitId
            newL!!.add(subRe)
            resourceList?.add(resourceObj)
            viewDelegate?.setListData(resourceList)

            val request = UnitReportAddResourceRequestObj()
            request.conetnt = resourceObj.conetnt
            request.resourceType = resourceObj.resourceType
            request.amount = resourceObj.amount
            request.endTime = endTime
            request.startTime = longTime
            request.unitId = unitId
            PartyManagerServer.unitReportAddResource(request)
        }
    }
}